var DataSourceTree = function(options) {
	this._data 	= options.data;
	this._delay = options.delay;
}

DataSourceTree.prototype.data = function(options, callback) {
	var self = this;
	var $data = null;

	if(!("name" in options) && !("type" in options)){
		$data = this._data;//the root tree
		callback({ data: $data });
		return;
	}
	else if("type" in options && options.type == "folder") {
		if("additionalParameters" in options && "children" in options.additionalParameters)
			$data = options.additionalParameters.children;
		else $data = {}//no data
	}
	
	if($data != null)//this setTimeout is only for mimicking some random delay
		setTimeout(function(){callback({ data: $data });} , parseInt(Math.random() * 500) + 200);

	//we have used static data here
	//but you can retrieve your data dynamically from a server using ajax call
	//checkout examples/treeview.html and examples/treeview.js for more info
};

////设置全局ajax
$.ajaxSetup({
	xhrFields: {
		withCredentials: true
	}
});


var tree_data;
findAllSecond();
function findAllSecond(){
	$.ajax({
		url: urlcore + "/api/secondCatalogue/selectSecondAll",
		type: "GET",
		async : false,
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				tree_data = jQuery.parseJSON(data.data);
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			
		}
	});
}

findAll();
function findAll(){
	$.ajax({
		url: urlcore + "/api/thirdCatalogue/findAll?roleId="+roleId,
		type: "GET",
		async: false,
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
		if (data.success == true) {
			$.each(data.data, function(i,n) {
				var html = '{"children":{';
				$.each(n.first.second.third, function(m,l) {
					if (m != 0) {
						html+=",";
					}
					if (l.select == 1) {
						myArray.push(l.data);
					}
					html+='"'+l.title+'":{"name":"'+l.title+'","type": "item","data":'+l.data+',"selected":'+l.select+'}';
				}); 
				html+="}}";
				html = jQuery.parseJSON(html);
				tree_data[''+n.first.title+'']['additionalParameters']['children'][''+n.first.second.title+'']['additionalParameters']=html
			});
		} else {
			alert(data.msg);
		}
 
		},
		error:function() {
			
		}
	});
}

var treeDataSource = new DataSourceTree({data: tree_data});

